package com.ruoyi.celoan.handanUtils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SM4;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.celoan.handanUtils.request.HanDanDataRequest;
import com.ruoyi.celoan.handanUtils.request.HanDanDataSelectRequest;
import com.ruoyi.celoan.handanUtils.request.TokenDataRequest;
import com.ruoyi.celoan.handanUtils.response.CountryPlatformResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HanDanBean {
    private static final Logger log = LoggerFactory.getLogger( HanDanBean.class);
    public static final String myPublicKey = "049ecc3b71e8e43ae240298f9f55de4b9ad3733236ad5a3782448fe8a7d0a22f8b116c98dd9904871037d20b131b1b4d8165ef534f25205d628c5474448b5fc15e";
    public static final String myPrivateKey = "00969996be634b00d61a465f27403d0e7346d9d930d008d0f19f840604e89592dc";
    public static String appId = "iv63koa14o06lw74";
    public static String appKey = "CCB004";
    public static String appSecret = "ujh9rrcc59k6es63";

    public static String dealResult(SM2 sm2, CountryPlatformResponse response) {
        String key2 = response.getKey();
        byte[] key1 = sm2.decryptFromBcd(key2, KeyType.PrivateKey);
        SM4 sM4 = SmUtil.sm4(key1);
        String data1 = sM4.decryptStr(response.getData());
        String digest1 = SmUtil.sm3(data1);
        SM2 sm2Response = new SM2(null, myPublicKey);
        boolean flag = sm2Response.verify(digest1.getBytes(), HexUtil.decodeHex(response.getSignatureData()));
        log.info("解密信息:" + data1);
        return data1;
    }

    public static HanDanDataRequest jiaMiPort(String requestData, String token) {
        SM2KeyPair sm2KeyPair = new SM2KeyPair();
        sm2KeyPair.setPrivateKey(myPrivateKey);
        sm2KeyPair.setPublicKey(myPublicKey);
        HanDanDataRequest request = new HanDanDataRequest();
        String digest = SmUtil.sm3(requestData);
        SM2 sm2 = new SM2(myPrivateKey, myPublicKey);
        byte[] sign = sm2.sign(digest.getBytes(), null);
        String signatureData = HexUtil.encodeHexStr(sign);
        String key1 = RandomUtil.randomString(16);
        SM4 sM4 = SmUtil.sm4(key1.getBytes());
        String data2 = sM4.encryptHex(requestData);
        SM2 sm2Req = new SM2(null, HanDanHttpUtils.publicKey());
        String key2 = sm2Req.encryptBcd(key1, KeyType.PublicKey);
        log.info("key1:" + key1);
        log.info("公钥加密key2：" + key2);
        request.setKey(key2);
        request.setSignatureData(signatureData);
        request.setRequestData(data2);
        request.setPublicKey(myPublicKey);
        request.setToken(token);
        log.info("数据加密后结果:" + JSONObject.toJSONString(request));
        return request;
    }

    public static HanDanDataSelectRequest jiaMiChaXunPort(String requestData, String token) {
        SM2KeyPair sm2KeyPair = new SM2KeyPair();
        sm2KeyPair.setPrivateKey(myPrivateKey);
        sm2KeyPair.setPublicKey(myPublicKey);
        HanDanDataSelectRequest request = new HanDanDataSelectRequest();
        String digest = SmUtil.sm3(requestData);
        SM2 sm2 = new SM2(myPrivateKey, myPublicKey);
        byte[] sign = sm2.sign(digest.getBytes(), null);
        String signatureData = HexUtil.encodeHexStr(sign);
        String key1 = RandomUtil.randomString(16);
        request.setRequestData(requestData);
        request.setPublicKey(myPublicKey);
        request.setToken(token);
        log.info("数据加密后结果:" + JSONObject.toJSONString(request));
        return request;
    }

    public static BasePlatformRequest jiaMiToken() {
        SM2KeyPair sm2KeyPair = new SM2KeyPair();
        sm2KeyPair.setPrivateKey(myPrivateKey);
        sm2KeyPair.setPublicKey(myPublicKey);
        BasePlatformRequest request = new BasePlatformRequest();
        TokenDataRequest tokenDataRequest = new TokenDataRequest();
        tokenDataRequest.setAppId(appId);
        tokenDataRequest.setAppKey(appKey);
        tokenDataRequest.setAppSecret(appSecret);
        String data1 = JSONObject.toJSONString(tokenDataRequest);
        log.info("获取token参数:" + data1);
        String digest = SmUtil.sm3(data1);
        assert digest != null;
        SM2 sm2 = new SM2(sm2KeyPair.getPrivateKey(), sm2KeyPair.getPublicKey());
        byte[] sign = sm2.sign(digest.getBytes(), null);
        String signatureData = HexUtil.encodeHexStr(sign);
        String key1 = RandomUtil.randomString(16);
        SM4 sM4 = SmUtil.sm4(key1.getBytes());
        String data2 = sM4.encryptHex(data1);
        SM2 sm2Req = new SM2(null, HanDanHttpUtils.publicKey());
        String key2 = sm2Req.encryptBcd(key1, KeyType.PublicKey);
        log.info("key1:" + key1);
        log.info("公钥加密key2：" + key2);
        request.setKey(key2);
        request.setSignatureData(signatureData);
        request.setRequestData(data2);
        request.setPublicKey(sm2KeyPair.getPublicKey());
        log.info("数据加密后结果:" + JSONObject.toJSONString(request));
        return request;
    }
}


/* Location:              D:\tuodayun\workspace\work\celoan-third-data-report-service-2.0.0-SNAPSHOT.jar!\BOOT-INF\classes\com\celoan\handanUtils\HanDanBean.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
